﻿using MediatR;
using TraceabilityApp_Api.Application.Command.DeepProcessing_深加工_;
using TraceabilityApp_Api.DTO;
using TraceabilityApp_Domain;
using TraceabilityApp_ErrorCode;
using TraceabilityApp_Infrastructure;

namespace TraceabilityApp_Api.Application.Handler.DeepProcessing_深加工_
{
    public class GetEnterpriseHandler : IRequestHandler<GetEnterpriseCommand, ApiResult<EnterpriseDTO>>
    {
        private readonly IBase<EnterpriseModel> enterpriseRepo;

        public GetEnterpriseHandler(IBase<EnterpriseModel> enterpriseRepo)
        {
            this.enterpriseRepo = enterpriseRepo;
        }
        public Task<ApiResult<EnterpriseDTO>> Handle(GetEnterpriseCommand request, CancellationToken cancellationToken)
        {
            ApiResult<EnterpriseDTO> result = new ApiResult<EnterpriseDTO>();
            var enterprise = enterpriseRepo.GetAll().Where(x => !x.IsDeleted);
            if(!string.IsNullOrEmpty(request.Batch))
            {
                enterprise=enterprise.Where(x =>x.Batch.Contains(request.Batch));
            }
            var linq = (from a in enterprise
                        select new EnterpriseDTO
                        {
                            Batch = a.Batch,
                            EnterpriseId = a.EnterpriseId,
                            EnterpriseName = a.EnterpriseName,
                            License = a.License,
                        }).FirstOrDefault(x=>x.Batch==request.Batch);
            result.Data = linq;
            result.Code = ApiEnum.OK;
            result.Msg = "深加工企业列表成功";
            return Task.FromResult(result);
        }
    }
}
